package page.ineffable.photostorage.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import page.ineffable.photostorage.pojo.User;
import page.ineffable.photostorage.service.UserService;
import page.ineffable.photostorage.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录控制
 *
 * @author lqs2
 * @date 2018/10/29, Mon
 */
@Controller
public class LoginController {


    private final UserService userService;
    private static Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    public LoginController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 进入首页，如果会话中存在此用户，直接进入
     *
     * @param session 会话
     **/
    @GetMapping({"/"})
    public String index(HttpSession session) {
        String un = (String) session.getAttribute("un");
        if (un != null) {
            return "redirect:/main";
        }
        return "login";
    }


    /**
     * 用户登录
     *
     * @param v0 用户名
     * @param v1 密码
     * @return 用户信息/0表示验证未通过
     **/
    @PostMapping("/login")
    @ResponseBody
    public String loginCheck(String v0, String v1, HttpServletRequest request, HttpSession session) {
        User user = userService.hasMatchUser(v0, v1);
        if (user == null) {
            return "0";
        }
        session.setAttribute("un", v0);
        session.setMaxInactiveInterval(1800);
        String currentUrlPrefix = request.getRequestURL().toString();
        currentUrlPrefix = currentUrlPrefix.substring(0, currentUrlPrefix.lastIndexOf("/"));
        return currentUrlPrefix + "/main";
    }

    /**
     * 前往个人相册中心
     *
     * @param session 会话
     **/
    @RequestMapping("/main")
    public ModelAndView goPersonalCenter(HttpSession session) {
        ModelAndView mav = new ModelAndView();
        String name = (String) session.getAttribute("un");
        if (StringUtils.isEmpty(name)) {
            mav.setViewName("login");
            return mav;
        }
        mav.setViewName("myineffable");
        User user = getAccountInfo(name);
        mav.addObject("username", user.getUsername());
        mav.addObject("quota_total", user.getLimit());
        mav.addObject("quota_used", user.getUsed());
        return mav;
    }

    /**
     * 是否存在此用户名的用户
     *
     * @param v0 用户名
     * @return 0：不存在，1：存在
     **/
    @GetMapping("/userCheck")
    @ResponseBody
    public String hasThisUser(String v0) {
        User user = getAccountInfo(v0);
        if (user == null) {
            return "0";
        }
        return "1";
    }

    /**
     * 用户注销登录，清除此次会话
     *
     * @param session 会话
     **/
    @PostMapping("/signOut")
    @ResponseBody
    public String userSignOut(HttpSession session) {
        if (session != null) {
            session.invalidate();
        }
        return "1";
    }

    /**
     * 获取用户信息
     *
     * @param username 用户名
     **/
    private User getAccountInfo(String username) {
        return userService.hasMatchUser(username);
    }


}
